<?php
/**
 * Permission Model
 * 
 * @author Dac Diep Vuong
 * @package acl.permission
 */
class Permission_model extends Model
{
    private $table = 'permission';
    
    /**
     * Add permission
     * 
     * @desc Tạo mới quyền
     * @param Array $data
     * @return Int | False
     */
    public function add($data) {
        if(!$data) return false;
        $result = $this->db->insert($this->table, $data);
        return $result;
    }
    
    public function update($data, $id = 0) {
        if(!$data || !$id) return false;
        $result = $this->db->update($this->table, $data, array('per_id' => $id));
        return $result;
    }
    
    /**
     * Delete permission
     * 
     * @desc Xóa quyền
     * @param Int $id
     * @return Int | False
     */
    public function del($id = 0) {
        if(!$id) return false;
        $result = $this->db->delete($this->table, "per_id = $id");
        return $result;
    }
    
    /**
     * Get list permission
     * 
     * @desc Danh sánh quyền
     * @return Array | False
     */
    public function getList() {  
        $this->db->select('per_id,name,code');
        $this->db->where_not_in('code', '');
        $this->db->orderby('order ASC');
        $result = $this->db->get($this->table);
        return $result->result_object();
    }
    
    public function getInfo($id) {
        if(!$id) return false;
        $result = $this->db->getwhere($this->table, "per_id = ". $id);
        return $result->row();
    }
    
    public function getGroup() {
        $this->db->where('order', 0); 
        $result = $this->db->get($this->table);
        return $result->result_object();
    }
    
    /**
     * Exits permission
     * 
     * @param String $code
     * @return Int
     */
    
    public function exitsPermission($code = '') {
        if(!trim($code)) return false;
        $this->db->select('COUNT(`per_id`)');
        $this->db->where('code', strtoupper($code));
        $result = $this->db->get($this->table);
        return $result->num_rows();  
    }
}  
?>
